/*
	Purpose/output: 1900 income scores at various levels.
*/

clear
set more off
cd "$Mydirectory1/1_DataSources/1900_IncomeScores/output"

***********************************************
 * OCC, OCC x RACE, AND OCC x RACE x SOUTH 
***********************************************

	use 1_PH1901_incomescores.dta, clear
	
	bysort fatheroccej race south_merge: keep if _n==1
	
	keep fatheroccej race south_merge income_PH_farmfix_byocc income_PH_farmfix_byrace income_PH_farmfix_byr_bys income_PH_farmfix_bysouth income_PH_just*
	tab fatheroccej race
	
	merge m:1 fatheroccej race south_merge using CensusAg1900_farmerincome_byrace_bysouth.dta
	assert fatheroccej==81 if _merge==3
	drop _merge 
	
	rename fatheroccej occ1950ej_PH //for future merging with survey data
	
	//Exclude non-working fathers in non-baseline variations
	foreach x of varlist income_PH_farmfix_byocc income_PH_farmfix_byrace income_PH_farmfix_bysouth income_PH_just*  {
		replace `x'=. if occ1950ej_PH==99
	}

	save IncomeScores_1900_byrace_bysouth.dta, replace
	
**-----------------------------------------------------------------------------
**-----------------------------------------------------------------------------

*****************************************
 * OCC x RACE x REGION 
*****************************************
	//Note: One cell with missing income.

	use 1_PH1901_incomescores.dta, clear
	drop if fatheroccej==99 //Exclude non-working fathers
	
	bysort fatheroccej race region_merge: keep if _n==1
	keep fatheroccej race region_merge income_PH_farmfix_byr_byreg 
		tab fatheroccej race
		
	merge m:1 fatheroccej race region_merge using CensusAg1900_farmerincome_byrace_byregion.dta
	assert fatheroccej==81 if _merge==3
	drop _merge 
	
	rename fatheroccej occ1950ej_PH
	
	save IncomeScores_1900_byrace_byregion.dta, replace
	
**-----------------------------------------------------------------------------
**-----------------------------------------------------------------------------

*****************************************
 * OCC x RACE x SOUTH x EDU
*****************************************
/*Note: Variation in Census of Agriculture estimates will 
        be generated by using 1940 Census ratios. */
	use 1_PH1901_incomescores.dta, clear
	drop if fatheroccej==99 
	
	bysort fatheroccej race south_merge edu: keep if _n==1
	keep fatheroccej race south_merge edu income_PH_farmfix_byr_bys_edu* 
	tab fatheroccej race
			
	//merge in race x south Census of Ag estimates 
	merge m:1 fatheroccej race south_merge using CensusAg1900_farmerincome_byrace_bysouth.dta
	assert fatheroccej==81 if _merge==3
	drop _merge netearn00_adj_byocc netearn00_adj_byrace
	
	//merge in 1940 ratios again in order to adjust Census of Ag estimates
	preserve
	use ../../CensusData/output/Ratios_1940_all_south_levels.dta, clear
	keep if fatheroccej==81
	
	bysort fatheroccej race south_merge edu: keep if _n==1
	keep fatheroccej race south_merge edu ratio_incwage_byr_bys_edu scale_factor_incwage
	
	tempfile tempie
	save `tempie'
	
	restore 

	merge 1:1 fatheroccej race south_merge edu using `tempie', assert(1 3) nogen
	
	//adjust by education---can use either ratio (varies by cell) or scale factor (constant within edu)
	gen netearn00_adj_byr_bys_edu = netearn00_adj_byr_bys * ratio
	label var netearn00_adj_byr_bys_edu "Farm earnings, adj. for edu. with ratio"
	
	gen netearn00_adj_byr_bys_edu_v2 = netearn00_adj_byr_bys * scale_factor
	label var netearn00_adj_byr_bys_edu_v2 "Farm earnings, adj. for eud. with constant scale factor"	
	
	drop ratio scale netearn00_adj_byr_bys	
	rename fatheroccej occ1950ej_PH

	save IncomeScores_1900_byrace_bysouth_byedu.dta, replace
